//
//  ClipViewController.m
//  TheVideoClip
//
//  Created by 北斗君合MacMini on 2018/3/29.
//  Copyright © 2018年 北斗君合MacMini. All rights reserved.
//

#import "ClipViewController.h"

#import "ClipContentView.h"

#import "TZImagePickerController.h"
#import "TZImageManager.h"
#import "TZVideoPlayerController.h"
#import "SRActionSheet.h"

@interface ClipViewController () <UINavigationControllerDelegate,TZImagePickerControllerDelegate> {
    PHAsset *_videoAsset;
}

@property (nonatomic, strong) ClipContentView *contentView;

@end

@implementation ClipViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _contentView = [[ClipContentView alloc] init];
    [self.view addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(kStatusAndNavigationBarHeight);
    }];
    
    __weak typeof (self) weakSelf = self;
    _contentView.inputVideoBlock = ^{
        [weakSelf pushImagePickerController];
    };
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"保存/播放" style:UIBarButtonItemStylePlain target:self action:@selector(saveCutVideoClick)];
    [self.navigationItem setRightBarButtonItem:rightItem];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 动画执行完成代理
- (void)didFinishTransition {
    
}

- (void)pushImagePickerController {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    // 2. 在这里设置imagePickerVc的外观
    //[imagePickerVc setBarItemTextColor:kColorTextDarkGrey];
    //[imagePickerVc setOKButtonTitleColorNormal:kColorTextDarkGrey];
    
    UINavigationBar *navigationBarAppearance;
    if (@available(iOS 9.0, *)) {
        navigationBarAppearance = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[TZImagePickerController class]]];
    } else {
        navigationBarAppearance = [UINavigationBar appearanceWhenContainedIn:[TZImagePickerController class], nil];
    }
    
    [navigationBarAppearance setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0x000000"]] forBarMetrics:UIBarMetricsDefault];
    [navigationBarAppearance setTintColor:[UIColor whiteColor]];//返回按钮的箭头颜色
    NSDictionary *textAttributes = @{
                                     NSFontAttributeName: [UIFont boldSystemFontOfSize:kNavTitleFontSize],
                                     NSForegroundColorAttributeName: [UIColor whiteColor],
                                     };
    [navigationBarAppearance setTitleTextAttributes:textAttributes];
    
    imagePickerVc.allowPickingVideo = YES;
    imagePickerVc.allowPickingImage = NO;
    imagePickerVc.allowTakePicture = NO;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    
    // You can get the photos by block, the same as by delegate.
    // 你可以通过block或者代理，来得到用户选择的照片.
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
    }];
    
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(id)asset {
    
    _videoAsset = asset;
    [_contentView completeInputVieo:asset];
}

- (void)saveCutVideoClick {
    if (!_videoAsset) {
        [MBProgressHUD showToast:@"请导入视频" view:_contentView];
        return;
    }
    [SRActionSheet sr_showActionSheetViewWithTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"保存", @"播放"] selectSheetBlock:^(SRActionSheet *actionSheetView, NSInteger index) {
        if (index == 0) {
            [_contentView saveCutVideoClick];
        } else {
            
        }
    }];
}

@end
